﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.BLL.BllExt;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Enum;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    /// <summary>
    /// 商家提现
    /// </summary>
    [Authorization]
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class WithdrawalsController : BaseController
    {
        public OrderExtBll OrderExtBllObj = new OrderExtBll();
        public ActionResult List()
        {
            return View();
        }
        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length, int shopId=0, int dealStatus = -1)
        {
            try
            {
                int order;
                int.TryParse(Request["order[0][column]"], out order);
                var search = Request["search[value]"];
                var direction = Request["order[0][dir]"];
                var orderByColumns = new[]
                {
                    "ApplyTime", "BankName", "BankAccount", "BankOfDeposit", "BankAccountName", "ss.Name", "CashValue","Remark",
                    "DealStatus"
                };
                var theList = McCashWithdrawApplicationBLL.Instance.GetPagingList(draw, (start / length) + 1, length,
                    SqlSafe(search), orderByColumns[order] + " " + direction, dealStatus, shopId, true);

                JsonResult jsonResult = Json(theList);
                jsonResult.MaxJsonLength = int.MaxValue;

                return jsonResult;
            }
            catch (Exception ex)
            {
                Logger.Error("WithdrawalsController Listing Catch Error: " + ex.Message);
                return Json(false);
            }
        }
        [HttpPost]
        public ActionResult Operation(McCashWithdrawApplication model)
        {
            var result = new Result() { Message = "操作失败", Status = false, StatusCode = 0 };
            var mcCashWithdrawApplication = McCashWithdrawApplicationBLL.Instance.GetModel(model.Id);
            if (mcCashWithdrawApplication == null)
            {
                return Json(result);
            }
            if (model.Type == 0)
            {
                mcCashWithdrawApplication.DealStatus = (int)CommonEnum.McWithdrawDealStatus.Success;
            }
            if (model.Type == 1)
            {
                mcCashWithdrawApplication.DealStatus = (int)CommonEnum.McWithdrawDealStatus.Faild;
            }
            mcCashWithdrawApplication.PayOptAccountId = CurrentUser.Id;

            McCashWithdrawApplicationBLL.Instance.Update(mcCashWithdrawApplication);
            result.Status = true;
            result.Message = "操作成功!";
            return Json(result);
        }
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection, McCashWithdrawApplication model)
        {
            var result = new Result();
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            double totalWithdrawCashByShopId = 0;
            //var lsWithDrawHistoryOfShop = McCashWithdrawApplicationBLL.Instance.GetTotalWithdrawCashByShopId(model.Id);
            //if (lsWithDrawHistoryOfShop != null && lsWithDrawHistoryOfShop.Count > 0)
            //{
            //    totalWithdrawCashByShopId = lsWithDrawHistoryOfShop.Sum(p => p.CashValue);
            //}
            //var nCashWithdrawalAmount = OrderExtBllObj.GetCashWithdrawalAmount(totalWithdrawCashByShopId,
            //    model.ShopId);

            var totalIncomeOfShopId = ShopsBLL.Instance.GetTotalIncomeOfShopId(model.ShopId);
            var totalShopCommission = ShopsBLL.Instance.GetTotalShopCommission(model.ShopId);
            var lsWithDrawHistoryOfShop = McCashWithdrawApplicationBLL.Instance.GetTotalWithdrawCashByShopId(model.ShopId);
            if (lsWithDrawHistoryOfShop != null && lsWithDrawHistoryOfShop.Count > 0)
            {
                totalWithdrawCashByShopId = lsWithDrawHistoryOfShop.Sum(p => p.CashValue);
            }
            var nCashWithdrawalAmount = (totalIncomeOfShopId + totalShopCommission) - totalWithdrawCashByShopId;//可提现金额
            if (nCashWithdrawalAmount < 1 || model.CashValue > nCashWithdrawalAmount)
            {
                result.Status = false;
                result.Message = "提现额度不足," + model.ShopName + "可提现金额为:" + nCashWithdrawalAmount + "元!";
                return Json(result);
            }
            model.BankName = "线下提现";
            model.PayTime = model.PayTime;
            model.PayOptAccountId = this.CurrentUser.Id;
            model.DealStatus = (int)CommonEnum.McWithdrawDealStatus.Success;
            model.IsDelete = false;
            result.Status = McCashWithdrawApplicationBLL.Instance.Add(model) > 0;
            if (result.Status == false)
            {
                result.Message = "添加失败，请重试";
            }
            return Json(result);
        }

        /// <summary>
        /// 搜索店铺
        /// </summary>
        /// <param name="keyWords"></param>
        /// <returns></returns>
           [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult GetShopList(string keyWords)
        {
            var result = new Result<List<Shops>>();
            var list = ShopsBLL.Instance.GetModelList(string.Format("Name like '%{0}%'", keyWords));
            if (list != null)
            {
                result = new Result<List<Shops>>
                {
                    Status = true,
                    Message = "Success",
                    Data = list
                };
            }
            else
            {
                result.Message = "店铺不存在！";
            }
            return Json(result);
        }
    }
}
